<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\cdc;

use app\enum\CdcEnum;
use app\enum\CommonEnum;
use app\model\mongo\MongoKsAccountSyncLogModel;
use app\model\mongo\MongoKsPicSyncLogModel;
use app\model\mongo\MongoKsVideoSyncLogModel;
use app\model\mongo\MongoOeAccountSyncLogModel;
use app\model\mongo\MongoOePicSyncLogModel;
use app\model\mongo\MongoOeVideoSyncLogModel;
use app\model\mongo\MongoQqAccountSyncLogModel;
use app\model\mongo\MongoQqPicSyncLogModel;
use app\model\mongo\MongoQqVideoSyncLogModel;
use app\model\mongo\MongoSyncModel;
use app\service\BaseService;
use app\service\cdc\report\account\KsAccountReportService;
use app\service\cdc\report\account\OeAccountReportService;
use app\service\cdc\report\account\QqAccountReportService;
use app\service\cdc\report\pic\KsPicReportService;
use app\service\cdc\report\pic\OePicReportService;
use app\service\cdc\report\pic\QqPicReportService;
use app\service\cdc\report\video\KsVideoReportService;
use app\service\cdc\report\video\OeVideoReportService;
use app\service\cdc\report\video\QqVideoReportService;

final class ReportService extends BaseService
{
    /**
     * 类型列表.
     */
    public array $typeList = [];

    /**
     * @var int 媒体ID
     */
    protected int $mediaId = 0;

    /**
     * @var int 任务类型
     */
    protected int $taskType = 0;

    private ?BaseService $service;

    public function __construct(int $mediaId = 0, int $taskType = 0)
    {
        $this->mediaId = $mediaId;
        $this->taskType = $taskType;
    }

    /**
     * 获取对象.
     */
    public function getObject()
    {
        if ($this->mediaId == CommonEnum::OE_MEDIA) {
            if ($this->taskType == CdcEnum::TASK_TYPE_ACCOUNT) {
                return $this->service = new OeAccountReportService();
            }

            if ($this->taskType == CdcEnum::TASK_TYPE_VIDEO) {
                return $this->service = new OeVideoReportService();
            }
            if ($this->taskType == CdcEnum::TASK_TYPE_PICTURE) {
                return $this->service = new OePicReportService();
            }
        } elseif ($this->mediaId == CommonEnum::KS_MEDIA) {
            if ($this->taskType == CdcEnum::TASK_TYPE_ACCOUNT) {
                return $this->service = new KsAccountReportService();
            }

            if ($this->taskType == CdcEnum::TASK_TYPE_VIDEO) {
                return $this->service = new KsVideoReportService();
            }
            if ($this->taskType == CdcEnum::TASK_TYPE_PICTURE) {
                return $this->service = new KsPicReportService();
            }
        } elseif ($this->mediaId == CommonEnum::QQ_MEDIA) {
            if ($this->taskType == CdcEnum::TASK_TYPE_ACCOUNT) {
                return $this->service = new QqAccountReportService();
            }

            if ($this->taskType == CdcEnum::TASK_TYPE_VIDEO) {
                return $this->service = new QqVideoReportService();
            }
            if ($this->taskType == CdcEnum::TASK_TYPE_PICTURE) {
                return $this->service = new QqPicReportService();
            }
        }

        return self::setError('未知媒体类型');
    }

    /**
     * 获取类型列表.
     */
    public function getTypeList()
    {
        if ($this->taskType == CdcEnum::TASK_TYPE_ACCOUNT) {
            return $this->typeList = $this->getAccountTypeList();
        }

        if ($this->taskType == CdcEnum::TASK_TYPE_VIDEO) {
            return $this->typeList = $this->getVideoTypeList();
        }

        if ($this->taskType == CdcEnum::TASK_TYPE_PICTURE) {
            return $this->typeList = $this->getPicTypeList();
        }

        return self::setError('未知类型');
    }

    /**
     * 获取列表.
     * @return mixed
     */
    public function getList(array $params = [])
    {
        return $this->service::getList($params);
    }

    /**
     * 运行修复.
     * @param MongoSyncModel $model 中间表模型
     */
    public function run(MongoSyncModel $model): bool
    {
        return $this->service->runStat($model);
    }

    /**
     * 更新同步状态
     * @param string $id 主键ID
     */
    public function updateSync(string $id = ''): bool
    {
        return $this->service::updateSync($id) <= 0;
    }

    /**
     * 账户类型列表.
     */
    protected function getAccountTypeList(): array
    {
        if ($this->mediaId == CommonEnum::OE_MEDIA) {
            return [
                MongoOeAccountSyncLogModel::TYPE_CLIENT,
                MongoOeAccountSyncLogModel::TYPE_PRODUCT,
                MongoOeAccountSyncLogModel::TYPE_MONTH,
                MongoOeAccountSyncLogModel::TYPE_CLIENT_MEDIA,
            ];
        }
        if ($this->mediaId == CommonEnum::KS_MEDIA) {
            return [
                MongoKsAccountSyncLogModel::TYPE_CLIENT,
                MongoKsAccountSyncLogModel::TYPE_PRODUCT,
                MongoKsAccountSyncLogModel::TYPE_MONTH,
                MongoKsAccountSyncLogModel::TYPE_CLIENT_MEDIA,
            ];
        }
        return [
            MongoQqAccountSyncLogModel::TYPE_CLIENT,
            MongoQqAccountSyncLogModel::TYPE_PRODUCT,
            MongoQqAccountSyncLogModel::TYPE_MONTH,
            MongoQqAccountSyncLogModel::TYPE_CLIENT_MEDIA,
        ];
    }

    /**
     * 图片类型列表.
     */
    protected function getPicTypeList(): array
    {
        if ($this->mediaId == CommonEnum::OE_MEDIA) {
            return [
                MongoOePicSyncLogModel::TYPE_ACCOUNT,
                MongoOePicSyncLogModel::TYPE_CLIENT,
                MongoOePicSyncLogModel::TYPE_PIC,
            ];
        }
        if ($this->mediaId == CommonEnum::KS_MEDIA) {
            return [
                MongoKsPicSyncLogModel::TYPE_ACCOUNT,
                MongoKsPicSyncLogModel::TYPE_CLIENT,
                MongoKsPicSyncLogModel::TYPE_KS_USER,
                MongoKsPicSyncLogModel::TYPE_PIC,
            ];
        }
        return [
            MongoQqPicSyncLogModel::TYPE_ACCOUNT,
            MongoQqPicSyncLogModel::TYPE_CLIENT,
            MongoQqPicSyncLogModel::TYPE_PIC,
        ];
    }

    /**
     * 视频类型列表.
     */
    protected function getVideoTypeList(): array
    {
        if ($this->mediaId == CommonEnum::OE_MEDIA) {
            return [
                MongoOeVideoSyncLogModel::TYPE_ACCOUNT,
                MongoOeVideoSyncLogModel::TYPE_CLIENT,
                MongoOeVideoSyncLogModel::TYPE_VIDEO,
            ];
        }
        if ($this->mediaId == CommonEnum::KS_MEDIA) {
            return [
                MongoKsVideoSyncLogModel::TYPE_ACCOUNT,
                MongoKsVideoSyncLogModel::TYPE_CLIENT,
                MongoKsVideoSyncLogModel::TYPE_KS_USER,
                MongoKsVideoSyncLogModel::TYPE_VIDEO,
            ];
        }

        return [
            MongoQqVideoSyncLogModel::TYPE_ACCOUNT,
            MongoQqVideoSyncLogModel::TYPE_CLIENT,
            MongoQqVideoSyncLogModel::TYPE_VIDEO,
        ];
    }

    /**
     * 删除数据
     * @param array $params 参数
     * @return mixed
     */
    public function deleteData(array $params = []){
        return $this->service::deleteData($params);
    }

    /**
     * 修改数据
     * @param array $params 参数
     * @param array $data 数据
     * @return mixed
     */
    public function updateData(array $params = [],array $data = []){
        return $this->service::updateData($params,$data);
    }

    /**
     * 获取信息
     * @param array $params 参数
     * @return mixed
     */
    public function getInfo(array $params = []){
        return $this->service::getInfo($params);
    }
}
